using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class Weapon : MonoBehaviour
{
    public PrefabEntity self;
    public AudioSource effectAudio;

    public float shake;
    public float attack;
    public float mp;

    public bool isMmelee = false;
    public bool equipped = false;
    public bool canPick = false;
    public bool isCommodity = false;

    protected float x, y;

    protected Quaternion[] quaternions =
    {
        Quaternion.Euler(0, 0, 45), //45°       0
        Quaternion.Euler(0, 0, -45), //-45°     1
        Quaternion.Euler(0, 0, 90), //90°       2
        Quaternion.Euler(0, 0, -90), //-90°     3
        Quaternion.Euler(0, 180, 45), //45°     4
        Quaternion.Euler(0, 180, -45), //-45°   5
        Quaternion.Euler(0, 180, 90), //90°     6
        Quaternion.Euler(0, 180, -90), //-90°   7
        Quaternion.Euler(0, 180, 0),
        Quaternion.Euler(0, 0, 0),
        Quaternion.Euler(0, 0, -135),
    };

    public Vector3 holdedPos = new Vector3(0, 0);
    public Vector3 rotateY = new Vector3(0, -90, 0);

    public string weaponName;

    public SpriteRenderer spriteRenderer;

    private Collider2D trigger;

    private void Awake()
    {
        effectAudio = GetComponent<AudioSource>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        trigger = GetComponent<Collider2D>();

        weaponName = name;
        canPick = !equipped;
        trigger.enabled = !isCommodity;
        InitData();
        
        InitEvent();
    }

    private void InitEvent()
    {
#if UNITY_ANDROID
        EasyJoystick.On_JoystickMove += OnJoystickMove;
        EasyButton.On_ButtonPress += OnAttackBtnPress;
#endif
    }
    
    private void OnEnable()
    {
        effectAudio.enabled = GameController.Instance().playerDataCtr.setting.EffectState;
        effectAudio.volume = GameController.Instance().playerDataCtr.setting.EffectValue * 0.01f;
        tag = transform.parent.tag;
        if (tag.Equals("Player")) transform.localPosition = holdedPos;
        trigger.enabled = !equipped;

    }

    private void OnDestroy()
    {

        GameController.Instance().audioCtr.effectAudios.Remove(effectAudio);
#if UNITY_ANDROID
        

        EasyButton.On_ButtonUp -= OnFuncBtnUp;
        EasyJoystick.On_JoystickMove -= OnJoystickMove;
        EasyButton.On_ButtonPress -= OnAttackBtnPress;
#endif
    }

    private void InitData()
    {
        GameController.Instance().audioCtr.effectAudios.Add(effectAudio);
    }

    protected void CheckKeysState()
    {
        if (tag.Equals("Player") && Input.GetButton("Fire1") && equipped)
        {
            if (GameController.Instance().FSM.CurPlayerState == PlayerState.Die) return;
            if (GameController.Instance().playerCtr.Player.mp < mp) return;
            AttackButtonPress();
        }
    }

    public abstract void AttackButtonPress();

    public virtual void AttackButtonUp()
    {
    }


    public virtual void UpdateLookAt(Vector3 target)
    {
        transform.right = (target - transform.position).normalized;
        if (transform.position.x > target.x)
        {
            spriteRenderer.flipY = true;
        }
        else if (transform.position.x < target.x)
        {
            spriteRenderer.flipY = false;
        }
    }

    public virtual void ResetForward()
    {
        spriteRenderer.flipX = false;
        spriteRenderer.flipY = false;
        transform.rotation = Quaternion.identity;
    }

    protected virtual void ChangeOrientation()
    {
        if (tag.Equals("Player") && GameController.Instance().FSM.CurPlayerState == PlayerState.NotFight)
        {
            x = Input.GetAxis("Horizontal");
            y = Input.GetAxis("Vertical");
            if (x > 0.001)
            {
                if (y > 0.001) transform.rotation = quaternions[0];
                else if (y < -0.001) transform.rotation = quaternions[1];
                else transform.rotation = Quaternion.identity;
            }
            else if (x < -0.001)
            {
                if (y > 0.001) transform.rotation = quaternions[4];
                else if (y < -0.001) transform.rotation = quaternions[5];
                else transform.rotation = quaternions[8];
            }
            else if (y > 0.001)
            {
                if (transform.rotation.y == 0)
                {
                    transform.rotation = quaternions[2];
                }
                else
                {
                    transform.rotation = quaternions[6];
                }
            }
            else if (y < -0.001)
            {
                if (transform.rotation.y == 0)
                {
                    transform.rotation = quaternions[3];
                }
                else
                {
                    transform.rotation = quaternions[7];
                }
            }
        }
    }

    public void UpdateWeaponLevelData()
    {
        GameController.Instance().archiveCtr.TempLevelData.weapon = self;
    }

    public void PickUp(GameObject go)
    {
        if (!canPick) return;
        equipped = true;
        trigger.enabled = false;
        tag = go.tag;
        transform.parent = go.transform.parent;
        transform.localPosition = holdedPos;
        transform.rotation = Quaternion.identity;
        GameController.Instance().weaponCtr.Weapon.PutDown();
        GameController.Instance().weaponCtr.Weapon = this;
#if UNITY_ANDROID
        EasyJoystick.On_JoystickMove += OnJoystickMove;
        EasyButton.On_ButtonPress += OnAttackBtnPress;
#endif
    }

    public void PutDown()
    {
        canPick = false;
        trigger.enabled = true;
        tag = "Untagged";
        equipped = false;
        ResetForward();

        // transform.parent = GameController.Instance().gameCore.transform;
        transform.parent = GameController.Instance().mapCtr.CurMap();
        StartCoroutine(SetPick());
#if UNITY_ANDROID
        EasyJoystick.On_JoystickMove -= OnJoystickMove;
        EasyButton.On_ButtonPress -= OnAttackBtnPress;
#endif
    }

    private IEnumerator SetPick()
    {
        yield return new WaitForSeconds(0.2f);
        canPick = true;
    }

    public string GetTipsInfo()
    {
        return weaponName;
    }

    public void OnTriggerStay2D(Collider2D other)
    {
        // print(other.name + "[" + other.gameObject.layer + "]--WeaponTrigger");
        if (Input.GetKey(KeyCode.E) && other.gameObject.layer.Equals(9))
        {
            print("Change Weapon -- Success.");
            PickUp(other.gameObject);
        }
    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.layer.Equals(9) && !equipped)
        {
            GameController.Instance().panelCtr.LoadWeaponPanel(self.Name, attack.ToString(), mp.ToString());
#if UNITY_ANDROID
            if(other.gameObject.layer.Equals(9)) {
                GameController.Instance().panelCtr.phonePanel.SwitchBtnsActive(true);
                EasyButton.On_ButtonUp += OnFuncBtnUp;
            }
#endif
        }
    }

    public void OnTriggerExit2D(Collider2D other)
    {
        GameController.Instance().panelCtr.RemoveWeaponPanel();
#if UNITY_ANDROID
        if(other.gameObject.layer.Equals(9)) {
            GameController.Instance().panelCtr.phonePanel.SwitchBtnsActive(false);
            EasyButton.On_ButtonUp -= OnFuncBtnUp;
        }
        
#endif
    }

    protected virtual void OnJoystickMove(MovingJoystick joystick)
    {
        if (!joystick.joystickName.Equals("PlayerJoystick") || !tag.Equals("Player") || gameObject.name.Contains("Scythe01") ||
            GameController.Instance().FSM.CurPlayerState == PlayerState.Fight) return;
        
        transform.LookAt(new Vector3(transform.position.x + joystick.joystickAxis.x,
            transform.position.y + joystick.joystickAxis.y, transform.position.z));
        if (gameObject.name.Contains("ShotGun"))
        {
            Quaternion quaternion = Quaternion.Euler(0.0f, joystick.joystickAxis.x > 0 ? 0 : 180, -transform.localEulerAngles.x);
            transform.rotation = quaternion;
        }
        else
        {
            transform.Rotate(rotateY);
        }
    }

    private void OnAttackBtnPress(string btn_name)
    {
        if (tag.Equals("Player") && btn_name.Equals("AttackBtn") && equipped)
        {
            if (GameController.Instance().FSM.CurPlayerState == PlayerState.Die) return;
            if (GameController.Instance().playerCtr.Player.mp < mp) return;
            AttackButtonPress();
        }
    }

    private void OnFuncBtnUp(string btn_name)
    {
        if (btn_name.Equals("FuncBtn") && !equipped)
        {
            print("Change Weapon -- Success.");
            PickUp(GameController.Instance().playerCtr.Player.gameObject);
        }
    }
}